const express = require('express');
const router = express.Router();
const { pool } = require('../config/database');

// 获取所有品牌
router.get('/list', async (req, res) => {
  try {
    const [rows] = await pool.execute(
      'SELECT * FROM brand WHERE is_deleted = 0 ORDER BY create_time DESC'
    );
    
    res.json({
      success: true,
      message: '获取品牌列表成功',
      data: rows
    });
  } catch (error) {
    console.error('获取品牌列表失败:', error);
    res.status(500).json({
      success: false,
      message: '获取品牌列表失败',
      error: error.message
    });
  }
});

// 根据ID获取品牌
router.get('/:id', async (req, res) => {
  try {
    const { id } = req.params;
    const [rows] = await pool.execute(
      'SELECT * FROM brand WHERE id = ? AND is_deleted = 0',
      [id]
    );
    
    if (rows.length === 0) {
      return res.status(404).json({
        success: false,
        message: '品牌不存在'
      });
    }
    
    res.json({
      success: true,
      message: '获取品牌信息成功',
      data: rows[0]
    });
  } catch (error) {
    console.error('获取品牌信息失败:', error);
    res.status(500).json({
      success: false,
      message: '获取品牌信息失败',
      error: error.message
    });
  }
});

// 添加品牌
router.post('/add', async (req, res) => {
  try {
    const { name, logo } = req.body;
    
    if (!name) {
      return res.status(400).json({
        success: false,
        message: '品牌名称不能为空'
      });
    }
    
    const [result] = await pool.execute(
      'INSERT INTO brand (name, logo) VALUES (?, ?)',
      [name, logo || null]
    );
    
    res.json({
      success: true,
      message: '添加品牌成功',
      data: {
        id: result.insertId,
        name,
        logo
      }
    });
  } catch (error) {
    console.error('添加品牌失败:', error);
    res.status(500).json({
      success: false,
      message: '添加品牌失败',
      error: error.message
    });
  }
});

// 更新品牌
router.put('/update/:id', async (req, res) => {
  try {
    const { id } = req.params;
    const { name, logo } = req.body;
    
    const [existingBrand] = await pool.execute(
      'SELECT * FROM brand WHERE id = ? AND is_deleted = 0',
      [id]
    );
    
    if (existingBrand.length === 0) {
      return res.status(404).json({
        success: false,
        message: '品牌不存在'
      });
    }
    
    await pool.execute(
      'UPDATE brand SET name = ?, logo = ? WHERE id = ?',
      [name, logo, id]
    );
    
    res.json({
      success: true,
      message: '更新品牌成功'
    });
  } catch (error) {
    console.error('更新品牌失败:', error);
    res.status(500).json({
      success: false,
      message: '更新品牌失败',
      error: error.message
    });
  }
});

// 删除品牌（软删除）
router.delete('/delete/:id', async (req, res) => {
  try {
    const { id } = req.params;
    
    const [existingBrand] = await pool.execute(
      'SELECT * FROM brand WHERE id = ? AND is_deleted = 0',
      [id]
    );
    
    if (existingBrand.length === 0) {
      return res.status(404).json({
        success: false,
        message: '品牌不存在'
      });
    }
    
    await pool.execute(
      'UPDATE brand SET is_deleted = 1 WHERE id = ?',
      [id]
    );
    
    res.json({
      success: true,
      message: '删除品牌成功'
    });
  } catch (error) {
    console.error('删除品牌失败:', error);
    res.status(500).json({
      success: false,
      message: '删除品牌失败',
      error: error.message
    });
  }
});

module.exports = router;